package com.imooc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.imooc.bo.CreateCompanyBO;
import com.imooc.bo.ModifyCompanyInfoBO;
import com.imooc.bo.QueryCompanyBO;
import com.imooc.bo.ReviewCompanyBO;
import com.imooc.enums.CompanyReviewStatus;
import com.imooc.enums.YesOrNo;
import com.imooc.feign.UserMicroServiceFeign;
import com.imooc.mapper.CompanyMapper;
import com.imooc.mapper.CompanyPhotoMapper;
import com.imooc.pojo.Company;
import com.imooc.pojo.CompanyPhoto;
import com.imooc.result.GraceJSONResult;
import com.imooc.service.CompanyService;
import com.imooc.utils.JsonUtils;
import com.imooc.utils.PagedGridResult;
import com.imooc.vo.CompanyInfoVO;
import com.imooc.vo.UsersVO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 企业表 服务实现类
 * </p>
 *
 * @author liansen
 * @since 2023-03-28
 */
@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {

    @Autowired
    private CompanyMapper companyMapper;

    @Autowired
    private UserMicroServiceFeign userMicroServiceFeign;

    @Autowired
    private CompanyPhotoMapper companyPhotoMapper;

    @Override
    public Company getByFullName(String fullName) {
        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<Company>().eq(Company::getCompanyName, fullName);
        return baseMapper.selectOne(wrapper);
    }

    @Override
    public String createCompany(CreateCompanyBO companyBO) {
        Company company = new Company();
        BeanUtils.copyProperties(companyBO, company);
        company.setIsVip(YesOrNo.NO.type);
        company.setReviewStatus(CompanyReviewStatus.NOTHING.type);
        company.setCreatedTime(LocalDateTime.now());
        company.setUpdatedTime(LocalDateTime.now());
        baseMapper.insert(company);
        return company.getId();
    }

    @Override
    public String resetCompanyReview(CreateCompanyBO companyBO) {
        Company company = new Company();
        BeanUtils.copyProperties(companyBO, company);
        company.setIsVip(YesOrNo.NO.type);
        company.setId(companyBO.getCompanyId());
        company.setReviewStatus(CompanyReviewStatus.NOTHING.type);
        company.setUpdatedTime(LocalDateTime.now());
        baseMapper.updateById(company);
        return companyBO.getCompanyId();
    }

    @Override
    public void commitReview(ReviewCompanyBO reviewCompanyBO) {
        Company company = new Company();
        company.setId(reviewCompanyBO.getCompanyId());
        company.setReviewStatus(CompanyReviewStatus.REVIEW_ING.type);
        company.setReviewReplay(""); //如果有内容, 则重置覆盖之前的审核意见
        company.setAuthLetter(reviewCompanyBO.getAuthLetter());
        company.setCommitUserId(reviewCompanyBO.getHrUserId());

        company.setCommitUserMobile(reviewCompanyBO.getHrMobile());
        company.setCommitDate(LocalDate.now());
        company.setUpdatedTime(LocalDateTime.now());

        baseMapper.updateById(company);
    }



    // --------------------------  运营端----------------------


    @Override
    public PagedGridResult queryCompanyListPaged(QueryCompanyBO companyBO, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<CompanyInfoVO> list = companyMapper.queryCompanyList(companyBO);
        PageInfo<CompanyInfoVO> pageInfo = new PageInfo<>(list);
        PagedGridResult result = new PagedGridResult();
        result.setRows(pageInfo.getList()); //每页显示的数据
        result.setPage(page); //当前页
        result.setTotal(pageInfo.getPages()); //总页数
        result.setRecords(pageInfo.getTotal()); //总记录数
        return result;
    }


    @Override
    public PagedGridResult getListAll(QueryCompanyBO companyBO, Integer page, Integer limit) {

        PageHelper.startPage(page, limit);

        LambdaQueryWrapper<Company> wrapper = new LambdaQueryWrapper<>();

        wrapper.orderByDesc(Company::getCommitDate);

        if(StringUtils.isNotBlank(companyBO.getCompanyName())) {
            wrapper.like(Company::getCompanyName, companyBO.getCompanyName());
        }

//        无法通过代码实现 通过申请人查询，只能通过sql的方式实现
//        if(StringUtils.isNotBlank(companyBO.getCommitUser())){
//            wrapper.like();
//        }

        if(companyBO.getReviewStatus() != null && companyBO.getReviewStatus() >= 0){
            wrapper.eq(Company::getReviewStatus, companyBO.getReviewStatus());
        }

        if(companyBO.getCommitDateStart() != null){
            wrapper.ge(Company::getCommitDate, companyBO.getCommitDateStart());
        }

        if(companyBO.getCommitDateEnd() != null){
            wrapper.le(Company::getCommitDate, companyBO.getCommitDateEnd());
        }


        List<Company> companies = baseMapper.selectList(wrapper);

        PageInfo<Company> pageInfo = new PageInfo<>(companies);

        List<CompanyInfoVO> collect = companies.stream().map(company -> {
            String commitUserId = company.getCommitUserId();
            GraceJSONResult result = userMicroServiceFeign.get(commitUserId);
            Object data = result.getData();
            String strJson = JsonUtils.objectToJson(data);
            UsersVO usersVO = JsonUtils.jsonToPojo(strJson, UsersVO.class);
            CompanyInfoVO companyInfoVO = new CompanyInfoVO();
            BeanUtils.copyProperties(company, companyInfoVO);
            companyInfoVO.setCommitUser(usersVO.getRealName());
            return companyInfoVO;
        }).collect(Collectors.toList());


//        PageInfo<CompanyInfoVO> pageInfo = new PageInfo<>(collect);


        PagedGridResult result = new PagedGridResult();


//        result.setRows(pageInfo.getList());

        PageInfo<CompanyInfoVO> companyInfoVOPageInfo = new PageInfo<>(collect);

        result.setRows(companyInfoVOPageInfo.getList());
        result.setPage(page);
        result.setTotal(pageInfo.getPages());
        result.setRecords(pageInfo.getTotal());

        return result;
    }

    @Override
    public CompanyInfoVO getCompanyInfoById(String companyId) {
        return companyMapper.getCompanyInfoById(companyId);
    }

    @Override
    public void reviewCompany(ReviewCompanyBO reviewCompanyBO) {
        Company company = new Company();
        company.setId(reviewCompanyBO.getCompanyId());
        company.setCommitUserId(reviewCompanyBO.getHrUserId());
        company.setReviewStatus(reviewCompanyBO.getReviewStatus());
        company.setReviewReplay(reviewCompanyBO.getReviewReplay());
        company.setUpdatedTime(LocalDateTime.now());
        baseMapper.updateById(company);
    }

    @Override
    @Transactional
    public void modify(ModifyCompanyInfoBO companyInfoBO) {
        Company company = new Company();
        company.setUpdatedTime(LocalDateTime.now());
        BeanUtils.copyProperties(companyInfoBO, company);
        company.setId(companyInfoBO.getCompanyId());
        baseMapper.updateById(company);
    }


    @Override
    @Transactional
    public void saveOrUpdatePhotos(ModifyCompanyInfoBO companyInfoBO) {

        String companyId = companyInfoBO.getCompanyId();
        LambdaQueryWrapper<CompanyPhoto> wrapper = new LambdaQueryWrapper<CompanyPhoto>().eq(CompanyPhoto::getCompanyId, companyId);
        CompanyPhoto before = companyPhotoMapper.selectOne(wrapper);

        CompanyPhoto companyPhoto = new CompanyPhoto();
        companyPhoto.setCompanyId(companyInfoBO.getCompanyId());
        companyPhoto.setPhotos(companyInfoBO.getPhotos());

        if(Objects.isNull(before)){
            companyPhotoMapper.insert(companyPhoto);
        }else {
            companyPhotoMapper.update(companyPhoto, new LambdaQueryWrapper<CompanyPhoto>().eq(CompanyPhoto::getCompanyId, companyId));
        }

    }

    @Override
    public CompanyPhoto getPhotos(String companyId) {
        LambdaQueryWrapper<CompanyPhoto> wrapper = new LambdaQueryWrapper<CompanyPhoto>().eq(CompanyPhoto::getCompanyId, companyId);
        return companyPhotoMapper.selectOne(wrapper);
    }
}


